package com.bawei.yangzongcai___day2.MVCAcyivity;



import android.content.Context;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.bawei.mvc.View.BmvcAcyivity;
import com.bawei.yangzongcai___day2.R;
import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.bumptech.glide.Glide;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;
import com.youth.banner.Banner;
import com.youth.banner.loader.ImageLoader;

import java.io.IOException;
import java.util.List;

import okhttp3.Response;

public class MainActivity2 extends BmvcAcyivity implements OkhttpMvc.MyCallBack, OnRefreshLoadMoreListener {



    private SmartRefreshLayout smartLayout;
    private RecyclerView lerview;
    private boolean isoffer=true;
    private int pash=3;
    private BaseMullseeAdpter mullseeAdpter;
    private BmvcModel model;


    @Override
    public int bandmvcLayout() {
        return R.layout.activity_main2;
    }

    @Override
    public void ininmvcView() {
        smartLayout = findViewById(R.id.smartLayout);
        lerview = findViewById(R.id.lerview);
        lerview.setLayoutManager(new StaggeredGridLayoutManager(1,StaggeredGridLayoutManager.VERTICAL));
        smartLayout.setOnRefreshLoadMoreListener(this);
    }

    @Override
    public void ininmvcDate() {
        model = new BmvcModel();
        model.inflertion(pash,this);
    }

    public void inconView(List<JavaMVC.DataBean> dataBeans)
    {
        smartLayout.finishRefresh();
        smartLayout.finishLoadMore();
        if (mullseeAdpter==null)
        {
            mullseeAdpter = new BaseMullseeAdpter(dataBeans);
            mullseeAdpter.addHeaderView(bannerview(dataBeans));
            lerview.setAdapter(mullseeAdpter);
        }else {
            if (isoffer)
            {
                mullseeAdpter.getData().clear();
            }
            mullseeAdpter.getData().addAll(dataBeans);
            mullseeAdpter.notifyDataSetChanged();
        }
    }

    public Banner bannerview (List<JavaMVC.DataBean>  sbaner)
    {
        Banner banner = new Banner(this);
        banner.setLayoutParams( new  ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, SizeUtils.dp2px(500)));
        banner.setImages(sbaner);
        banner.setImageLoader(new ImageLoader() {
            @Override
            public void displayImage(Context context, Object path, ImageView imageView) {
                JavaMVC.DataBean dataBean = (JavaMVC.DataBean) path;
                Glide.with(context).load(dataBean.getUrl()).into(imageView);
            }
        }).start();
        return banner;
    }


    @Override
    public void suuess(Response response) {
         JavaMVC javaMVC = null;
        try {
            javaMVC = GsonUtils.fromJson(response.body().string(),JavaMVC.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
        int chen =0;
        List<JavaMVC.DataBean> dataBeans = javaMVC.getData();
        for (JavaMVC.DataBean dataBean : dataBeans) {
            dataBean.setTatylayout(++chen);
            if (chen==3)
            {
                chen=0;
            }
        }
        inconView(dataBeans);
    }

    @Override
    public void fiad(String fiai) {

    }


    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        isoffer=false;
        pash++;
        if (pash==6)
        {
            pash=1;
        }
        model.inflertion(pash,this);
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        if (isoffer)
        {
            pash=1;
        }
        model.inflertion(pash,this);
    }



}
